package com.pro.h5case.admin.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pro.h5case.admin.service.AdminService;
import com.pro.h5case.common.model.BAdmin;
import com.pro.h5case.common.model.BDictionary;
import com.pro.h5case.common.model.BPlan;
import com.pro.h5case.common.service.BDictionaryService;
import com.pro.h5case.admin.service.PlanManageService;

@Controller
@RequestMapping("/samePlanManage")
public class SamePlanManageController {
	@Resource
	private PlanManageService planManageService;
	@Resource
	private BDictionaryService dictionaryService;
	@Resource
	private AdminService adminService;

	
	/**
	 * 列表页面
	 */
	@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
	public String list(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			Map<String,Object> parameter = new HashMap<String, Object>();
			parameter.put("dicType", "HANGYE");
			List<BDictionary> hangyeList = dictionaryService.selectByMap(parameter);
			model.addAttribute("hangyeList", hangyeList);
			parameter.clear();
			parameter.put("dicType", "JIERI");
			List<BDictionary> jieriList = dictionaryService.selectByMap(parameter);
			model.addAttribute("jieriList", jieriList);
			parameter.clear();
			parameter.put("dicType", "GONGNENG");
			List<BDictionary> gongnengList = dictionaryService.selectByMap(parameter);
			model.addAttribute("gongnengList", gongnengList);
			parameter.clear();
			parameter.put("begin", 0);
			parameter.put("size", 15);
			List<BPlan> planList = planManageService.selectSamePlanByMap(parameter);
			 hangyeList.addAll(jieriList);
			 hangyeList.addAll(gongnengList);
			//替换 tag
			for (int i = 0; i < planList.size(); i++) {
				String [] tags = planList.get(i).getPlanTag().split(",");
				String varTag="";
				if(tags.length>0){
					for (int j = 0; j < tags.length; j++) {
						for (int k = 0; k < hangyeList.size(); k++) {
							String hangye = hangyeList.get(k).getDicCode();
							if(hangye.equals(tags[j])){
								varTag +=hangyeList.get(k).getDicName() + ",";
							}
						}
					}
				}
				
				planList.get(i).setPlanTag(varTag);
			}
			model.addAttribute("planList", planList);
			int planSum = planManageService.countSamePlanByMap(parameter);
			int pageSum = planSum / 15;
			if(planSum%15 == 0){
				
			}else{
				pageSum+=1;
			}
			model.addAttribute("pageNo", 1);
			model.addAttribute("pageSum", pageSum);
			model.addAttribute("planSum", planSum);
			return "admin/planManage/samelist";
	}
	/**
	 * 翻页
	 */
	@RequestMapping(value = "/newPage", method = RequestMethod.POST)
	@ResponseBody
	public Object newPage(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@RequestParam("pageNo") int pageNo,@RequestParam("key") String key,@RequestParam("tag") String tag,@RequestParam("audit") String audit)
			throws Exception {
		BAdmin ad =  (BAdmin) session.getAttribute("admin");
		
			Map<String,Object> parameter = new HashMap<String, Object>();
			parameter.clear();
			parameter.put("begin", pageNo*15);
			parameter.put("size", 15);
			parameter.put("sort", key);
			parameter.put("planTag", tag);
			parameter.put("planAudit", audit);
			List<BPlan> planList = planManageService.selectSamePlanByMap(parameter);
			model.addAttribute("planList", planList);
			
			int planSum = planManageService.countSamePlanByMap(parameter);
			//替换 tag
			parameter.clear();
			List<BDictionary> hangyeList = dictionaryService.selectByMap(parameter);
			for (int i = 0; i < planList.size(); i++) {
				String [] tags = planList.get(i).getPlanTag().split(",");
				String varTag="";
				if(tags.length>0){
					for (int j = 0; j < tags.length; j++) {
						for (int k = 0; k < hangyeList.size(); k++) {
							String hangye = hangyeList.get(k).getDicCode();
							if(hangye.equals(tags[j])){
								varTag +=hangyeList.get(k).getDicName() + ",";
							}
						}
					}
				}
				planList.get(i).setPlanTag(varTag);
			}
			
			int pageSum = planSum / 15;
			if(planSum%15 == 0){
				
			}else{
				pageSum+=1;
			}
			parameter.clear();
			parameter.put("planList", planList);
			parameter.put("pageSum", pageSum);
			parameter.put("pageNo", pageNo);
			parameter.put("planSum", planSum);
			return parameter;
		
	}
	/**
	 * 修改案例
	 */
	@RequestMapping(value = "/toUpdate", method = RequestMethod.GET)
	public String toUpdate(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@RequestParam("planId") int planId)
			throws Exception {
			Map<String,Object> parameter = new HashMap<String, Object>();
			parameter.put("dicType", "HANGYE");
			List<BDictionary> hangyeList = dictionaryService.selectByMap(parameter);
			model.addAttribute("hangyeList", hangyeList);
			parameter.clear();
			parameter.put("dicType", "JIERI");
			List<BDictionary> jieriList = dictionaryService.selectByMap(parameter);
			model.addAttribute("jieriList", jieriList);
			parameter.clear();
			parameter.put("dicType", "GONGNENG");
			List<BDictionary> gongnengList = dictionaryService.selectByMap(parameter);
			model.addAttribute("gongnengList", gongnengList);
			BPlan plan = planManageService.selectByPrimaryKey(planId);
			model.addAttribute("plan", plan);
			return "admin/planManage/update";
		
	}
	
	/**
	 * 修改案例
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String updatePlan(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,BPlan plan)
			throws Exception {
		BAdmin ad =  (BAdmin) session.getAttribute("admin");
		if(ad==null ){	
			response.getWriter().write("<script type='text/javascript'> window.top.location='../admin/login'</script>");
			return null;
		}
			int tag = planManageService.updatePlan(plan);
			model.addAttribute("upTag", tag);
			if(tag == 0){
				model.addAttribute("upTag", -1);
			}
			return "forward: list";
		
	}
	/**
	 * 删除
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public String deletePlan(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@RequestParam("id[]") int [] ids)
			throws Exception {
		
			int num = 0;
			BPlan plan = null;
			out:for (int i = 0; i < ids.length; i++) {
				plan = planManageService.selectByPrimaryKey(ids[i]);
				if("已审核".equals(plan.getPlanAudit())){
					num = -2;
					break out;
				}
				num = planManageService.deleteByPrimaryKey(ids[i]);
				if(num == 0){
					num = -1;
					break out;
				}
			}
			
			model.addAttribute("deleteTag", num);
			return "forward: list";
		
	}
	/**
	 * 审核
	 */
	@RequestMapping(value = "/approve", method = RequestMethod.POST)
	public String auditSelect(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@RequestParam("id[]") int [] ids)
			throws Exception {
		
			int num = 0;
			for (int i = 0; i < ids.length; i++) {
				num = planManageService.auditByPrimaryKey(ids[i]);
			}
			
			model.addAttribute("auditTag", num);
			if(num == 0){
				model.addAttribute("auditTag", -1);
			}
			return "forward: list";
		
	}
	/**
	 * 下线
	 */
	@RequestMapping(value = "/offline", method = RequestMethod.POST)
	public String offline(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@RequestParam("id[]") int [] ids)
			throws Exception {
		
			int num = 0;
			for (int i = 0; i < ids.length; i++) {
				num = planManageService.offlineByPrimaryKey(ids[i]);
			}
			
			model.addAttribute("offTag", num);
			if(num == 0){
				model.addAttribute("offTag", -1);
			}
			return "forward: list";
		
	}
	/**
	 * 预览案例
	 */
	@RequestMapping(value = "/see", method = RequestMethod.POST)
	@ResponseBody
	public Object see(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@RequestParam("planId") int planId)
			throws Exception {
			BPlan plan = planManageService.selectByPrimaryKey(planId);
			Map<String,Object> parameter = new HashMap<String, Object>();
			List<BDictionary> hangyeList = dictionaryService.selectByMap(parameter);
			
				String [] tags = plan.getPlanTag().split(",");
				String varTag="";
				if(tags.length>0){
					for (int j = 0; j < tags.length; j++) {
						for (int k = 0; k < hangyeList.size(); k++) {
							String hangye = hangyeList.get(k).getDicCode();
							if(hangye.equals(tags[j])){
								varTag +=hangyeList.get(k).getDicName() + "  ";
							}
						}
					}
				}
				plan.setPlanTag(varTag);
			return plan;
		
	}
	/**
	 * 修改热度及观看数
	 */
	@RequestMapping(value = "/updateHotOrWatch", method = RequestMethod.POST)
	@ResponseBody
	public Object updateHotOrWatch(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response,@RequestParam("planId") int planId,@RequestParam("tagName") String tagName,@RequestParam("num") int num)
			throws Exception {
			int tag = -1;
			BPlan plan = planManageService.selectByPrimaryKey(planId);
			if("hot".equals(tagName)){
				plan.setPlanHot(num);
				tag = planManageService.updatePlan(plan);
				
			}else if("watch".equals(tagName)){
				plan.setPlanWatch(num);
				tag = planManageService.updatePlan(plan);
			}
			return tag;
		
	}
	
}
